package com.tianqu.tms.www.permission.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.taglib.MenuBean;
import com.tianqu.tms.core.taglib.MenuTag;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.dto.UserFunctionDto;
import com.tianqu.tms.entity.UserClass;
/**
 * @author zhangxueliang 权限添加录入controller
 */
public class PermissionCreationInputController extends TmsController {
	private Logger logger = Logger
			.getLogger(PermissionCreationInputController.class);
	public final static String USERFUNCTIONLIST = "userFunctionList";
	public final static String USERCLASSLIST = "userClassList";
	public static final String CFUNCIDSTR = "cFuncIdStr";

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			MenuBean bean = uc.getRootMenuBean();
			List<MenuBean> functionList = new ArrayList<MenuBean>();
			List codesList = new ArrayList();
			functionList = 	bean.getChildrenList();
			String checkedStr = (String)uc.getAttribute(CFUNCIDSTR);	
			for(MenuBean menuBean : functionList){
				if(menuBean != null){
					if(menuBean.getLevel() == 1){
						UserFunctionDto dto = new UserFunctionDto();
						dto.setFuncId(menuBean.getFunctionId());
						dto.setFuncName(menuBean.getMenuName());
						dto.setFuncGrade(1);//父
						codesList.add(dto);
						List<MenuBean> menuBeanCList = menuBean.getChildrenList();
						if(menuBeanCList.size() > 0 && menuBeanCList != null){
							int i = 1;
							for(MenuBean menuBeanC : menuBeanCList){
								UserFunctionDto dtoC = new UserFunctionDto();
								if(!StringUtil.isNullOrBlank(checkedStr)){
									if(checkedStr.indexOf(menuBeanC.getFunctionId()) > -1){
										dtoC.setChecked(1);
									}else{
										dtoC.setChecked(2);
									}
								}
								
								if(i % 2 == 0){
									dtoC.setTrFlag(2);
								}else{
									dtoC.setTrFlag(1);
								}
								if(menuBeanCList.size() == 1){//如果这一行只有一列，那么td要加上colspan="5"，并结束
									dtoC.setTrFlag(3);
								}
								dtoC.setFuncId(menuBeanC.getFunctionId());
								dtoC.setFuncName(menuBeanC.getMenuName());
								dtoC.setFuncGrade(2);//子
								codesList.add(dtoC);
								i++;
							}
						}
					}
				}
			}
			

			UserClass userClass = UserClass.getInstance();
			List userClassList = userClass.getUserClassList();
			uc.setAttribute(USERFUNCTIONLIST, codesList);
			uc.setAttribute(USERCLASSLIST, userClassList);

			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
